<template>
<div class="pwd-container">
  <el-dialog title="修改密码" :visible.sync="pwdDialog" :append-to-body="true" :before-close="clearForm" width="30%">
    <el-form :model="pwdForm" status-icon :rules="pwdRules" ref="pwdForm" label-width="100px" label-position="left" class="demo-ruleForm">
      <el-form-item label="旧密码" prop="oldpassword">
        <el-input type="password" v-model="pwdForm.oldpassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword">
        <el-input type="password" v-model="pwdForm.newpassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="renewpassword">
        <el-input type="password" v-model="pwdForm.renewpassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('pwdForm')">提交</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</div>
</template>

<script>
import {
  changePwdApi
} from "@/api/ucenter/user";

export default {
  data() {
    var pwd = new RegExp("(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{8,16}");

    var oldPwd = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入旧密码"));
      } else {
        callback();
      }
    };
    var newPwd = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入新密码"));
      } else if (!pwd.test(value)) {
        callback(new Error("密码长度为8到16位且必须包含大小写字母和数字"));
      } else {
        if (this.pwdForm.renewpassword !== "") {
          this.$refs.pwdForm.validateField("checkPwd");
        }
        callback();
      }
    };
    var checkPwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.pwdForm.newpassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      pwdDialog: false,
      pwdForm: {
        oldpassword: "",
        newpassword: "",
        renewpassword: "",
      },
      pwdRules: {
        oldpassword: [{
          required: true,
          validator: oldPwd,
          trigger: "blur"
        }],
        newpassword: [{
          required: true,
          validator: newPwd,
          trigger: "blur"
        }],
        renewpassword: [{
          required: true,
          validator: checkPwd,
          trigger: "blur"
        }],
      },
    };
  },
  methods: {
    showDialog() {
      this.pwdDialog = true;
    },

    // 提交
    submitForm(formName) {
      let query = this.pwdForm
      this.$refs[formName].validate((valid) => {
        if (valid) {
          changePwdApi(query).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.pwdDialog = false;
              this.clearForm();
            } else {
              this.$message.error(res.msg)
            }
          });
        } else {
          this.$message.error('请检查您的填写')
          return false;
        }
      });
    },
    // 取消
    closeDialog() {
      this.pwdDialog = false;
      this.clearForm();
    },
    // 关闭弹窗清楚内容
    clearForm() {
      this.pwdDialog = false;
      this.$refs["pwdForm"].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
